package utp.tepc.usina;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Jackson
 * @version 1.0
 * @created 09-jun-2011 20:30:21
 */
public class Fabrica
{
    private List<Reator> reatores;
    private List<Monitor> monitores;
    private int totalTurno;
    private int turnoAtual;

    public Fabrica() throws Exception
    {
	this.turnoAtual = 0;
	this.monitores = new ArrayList<Monitor>();
	this.reatores = new ArrayList<Reator>();
	
	this.monitores.add(new Monitor());
	        
	OtimoPressao otpRa = new OtimoPressao();
	otpRa.setMax(1);
	otpRa.setMin(1);	
	OtimoTemperatura ottRa = new OtimoTemperatura();
	ottRa.setMax(30);
	ottRa.setMin(20);
	Reator RA = new Reator("RA", otpRa, ottRa);
	ottRa.setReator(RA);
	otpRa.setReator(RA);
	
	OtimoPressao otpRb = new OtimoPressao();
	otpRb.setMax(1.1f);
	otpRb.setMin(1.1f);	
	OtimoTemperatura ottRb = new OtimoTemperatura();
	ottRb.setMax(65);
	ottRb.setMin(40);
	Reator RB = new Reator("RB", otpRb, ottRb);
	ottRb.setReator(RB);
	otpRb.setReator(RB);
	
	OtimoPressao otpRc = new OtimoPressao();
	otpRc.setMax(1.5f);
	otpRc.setMin(1.5f);	
	OtimoTemperatura ottRc = new OtimoTemperatura();
	ottRc.setMax(170);
	ottRc.setMin(100);
	Reator RC = new Reator("RC", otpRc, ottRc);
	ottRc.setReator(RC);
	otpRc.setReator(RC);
	
	OtimoPressao otpRd = new OtimoPressao();
	otpRd.setMax(3);
	otpRd.setMin(3);	
	OtimoTemperatura ottRd = new OtimoTemperatura();
	ottRd.setMax(236);
	ottRd.setMin(210);
	Reator RD = new Reator("RD", otpRd, ottRd);
	ottRd.setReator(RD);
	otpRd.setReator(RD);
	
	this.reatores.add(RA);
	this.reatores.add(RB);
	this.reatores.add(RC);
	this.reatores.add(RD);
	
	RA.registerObserver(this.monitores.get(0));
	RB.registerObserver(this.monitores.get(0));
	RC.registerObserver(this.monitores.get(0));
	RD.registerObserver(this.monitores.get(0));
    }

    public void setReator(Reator reator) throws Exception
    {
	if (reator == null)
	    throw new Exception("Reator não pode ser Nulo.");

	if (this.reatores == null)
	    this.reatores = new ArrayList<Reator>();

	this.reatores.add(reator);
    }

    public void setMonitores(Monitor monitor) throws Exception
    {
	if (monitor == null)
	    throw new Exception("Monitor não pode ser Nulo.");

	if (this.monitores == null)
	    this.monitores = new ArrayList<Monitor>();

	this.monitores.add(monitor);
    }

    public void setTotalTurno(int totalTurno)
    {
	this.totalTurno = totalTurno;
    }

    public void iniciarTurno() throws Exception
    {
	while (this.totalTurno > 0)
	{
	    if (this.time())
	    {
		System.out.println(String.format("----------------------------------------------------- TURNO:%3d -----------------------------------------------------", ++this.turnoAtual));
		for(int i = 0; i < this.reatores.size(); i++)
		    this.reatores.get(i).turno();
		System.out.println("---------------------------------------------------------------------------------------------------------------------");
		this.totalTurno--;
	    }
	}
    }

    private boolean time()
    {
	long inicio = System.currentTimeMillis();
	long fim = 0;

	while ((fim - inicio) < 1000)
	    fim = System.currentTimeMillis();

	return true;
    }
}